using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public enum TimeState
{
    NotWork,    //无工作
    Working,    //工作中
    Done,       //工作完成
}

public class GameTimer
{
    /*
    1.计时时长
    2.计时结束后执行任务
    3.当前计时器状态
    4.是否停止当前计时器
    */
    private float startTime;
    private Action task;
    private TimeState timeState;
    private bool isStopTimer;

    public GameTimer()
    {
        ResetTimer();
    }

    public void StartTimer(float time, Action task)
    {
        this.startTime = time;
        this.task = task;
        this.isStopTimer = false;
        this.timeState = TimeState.Working;
    }

    public void UpdateTimer()
    {
        if(isStopTimer)
            return;
        
        startTime -= Time.deltaTime;
        if(startTime <= 0)
        {
            task?.Invoke();
            timeState = TimeState.Done;
            isStopTimer = true;
        }
    }

    public TimeState GetTimeState() => timeState;

    public void ResetTimer()
    {
        startTime = 0f;
        task = null;
        isStopTimer = true;
        timeState = TimeState.NotWork;
    }
}
